﻿using Volo.Abp.Application.Dtos;
using SanJ.FreeCare.ObservationDefinitions;
using SanJ.FreeCare.Peis.Configure.RoomPage;
using System;
using System.Threading.Tasks;
using System.Collections.Generic;
using SanJ.FreeCare.Locations;

namespace SanJ.FreeCare.Peis
{
    public partial class PeisAppService : FreeCareAppService, IPeisAppService
    {
        public async Task<PagedResultDto<RoomDto>> GetRoomListAsync(GetRoomInput input)
        {
            ///校验的逻辑
            var totalCount = await _locationManager.GetCountAsync(input.FilterText);
            var items = await _locationManager.GetPageLisAsync(input.FilterText, input.Sorting, input.MaxResultCount, input.SkipCount);

            return new PagedResultDto<RoomDto>
            {
                TotalCount = totalCount,
                Items = ObjectMapper.Map<List<Locations.Location>,List<RoomDto>>(items)
            };
        }
        public async Task<RoomDto>GetRoomAsync(Guid id)
        {
            var ob = await _locationManager.GetByIdAsync(id);

            return ObjectMapper.Map<Locations.Location, RoomDto>(ob);
        }
        public async Task<RoomDto>CreateRoomAsync(CreateRoomDto input)
        {
            Location lc = ToLocationAsync(input);
            var insert = await _locationManager.CreateAsync(lc);
            RoomDto result = new RoomDto();
            return result;

        }
        public async Task<RoomDto> UpdateRoomAsync(UpdateRoomDto input)
        {
            Location lc = ToLocationAsync(input);
            var update = await _locationManager.UpdateAsync(input.Id,lc);
            var result = ToRoomDto(lc);
            return result;
        }
        public  Task DeleteRoomAsync(Guid id)
        {
            return  _locationManager.Delete(id);
        }
        private RoomDto ToRoomDto(Location lc)
        {
            RoomDto room= new RoomDto();
            room.Description = lc.Description;
            room.Name = lc.Name;
            room.Code = lc.Code;
            room.Spell = lc.Spell;
            room.Type = lc.Type;
            room.State = lc.OperationalStatus;
            return room;
        }

        private Location ToLocationAsync(CreateRoomDto input)
        {
            Location lc = new Location();
            lc.Spell = input.Spell;
            lc.Code = input.Code;
            lc.Name = input.Name;
            lc.Description = input.Description;
            lc.Type = input.Type;
            lc.OperationalStatus= input.State;
            lc.Sort = input.DisplayOrder;
            return lc;
        }

        
    }


}
